package com.zhai.trigger.job;

import com.zhai.domain.strategy.model.valobj.StrategyAwardStockKeyVO;
import com.zhai.domain.strategy.service.IRaffleStock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Description: 更新奖品库存
 * @ClassName: UpdateAwardStockJob
 * @Author: zhaiyongxin
 * @Date: 2025/2/6 14:41
 * @Version: 1.0
 */
@Component()
@Slf4j
public class UpdateAwardStockJob {

    @Resource
    IRaffleStock raffleStock;

    @Scheduled(cron = "0/5 * * * * ?")
    public void exec(){
        try {
            //log.info("定时任务，更新奖品消耗库存【延迟队列获取，降低对数据库的更新频次，不要产生竞争】");
            StrategyAwardStockKeyVO strategyAwardStockKeyVO = raffleStock.takeQueueValue();
            if (strategyAwardStockKeyVO == null) {
                return;
            }

            log.info("定时任务-更新奖品消耗库存 strategyId:{} awardId:{}", strategyAwardStockKeyVO.getStrategyId(), strategyAwardStockKeyVO.getAwardId());
            raffleStock.updateStrategyAwardStock(strategyAwardStockKeyVO.getStrategyId(), strategyAwardStockKeyVO.getAwardId());
        } catch (Exception e) {
            log.error("定时任务-更新奖品消耗库存失败",e);
        }
    }
}
